/*
 *  Copyright 2010 mazhao.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package shopping.management;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import shopping.commons.BaseAction;

/**
 *
 * @author mazhao
 */
public class ProductShowImageAction extends BaseAction{
    private Log log = LogFactory.getLog(this.getClass());

    private String path;

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

     public InputStream getInputStream() throws Exception {
        
         String fullPath = getText(ProductAction.KEY_PRODUCT_IMAGE_DIR) + path;
         
         // if file exist show the specificed file.
         // else show default file.
         File targetFile = new File(fullPath);
         if(targetFile.isDirectory()) {
             log.debug("file is directory, then choose default file. the folder is " + fullPath);
             File[] files = targetFile.listFiles(new FileFilter(){

                public boolean accept(File pathname) {
                    String fileName = pathname.getName();
                    String fileexts = fileName.substring(fileName.indexOf("."));
                    return ManagementConstants.IMAGE_FILE_EXTS.contains(fileexts);
                }
            });

            log.debug("threre are " + files.length + " files in the folder");

            if(files.length > 0) {
                fullPath = files[0].getAbsolutePath();
                log.debug("choose default file " + fullPath);
            }
         }

         log.debug("file full path:" + fullPath);

         return new FileInputStream(fullPath);
    }

    @Action(
        value="/management/productshowimage",
        results ={
            @Result(name="success",
                location ="/management/productlist",
                type="stream",
                params={"contentType", "image/jpeg",
                    "inputName", "inputStream",
                    "bufferSize", "4096",
                    "contentDisposition", "filename=\"product.jpg\""
                }
            )
        }
    )
    public String show() throws Exception {
        return SUCCESS;
    }
}
